package com.alog.yzd.bookmall.service.order;

import com.alog.yzd.bookmall.dao.order.OrderDao;
import com.alog.yzd.bookmall.domain.order.Order;
import com.alog.yzd.bookmall.pager.PageBaen;
import com.alog.yzd.bookmall.utils.jdbc.JdbcUtils;

import java.sql.SQLException;

public class OrderService {
    private OrderDao orderDao = new OrderDao();

    /**
     * 按状态查询订单
     * @param pc
     * @return
     */
    public PageBaen<Order> findByStatus(int status, int pc){
        try {
            JdbcUtils.beginTransaction();
            PageBaen<Order> pb = orderDao.findByStatus(status, pc);
            JdbcUtils.commitTransaction();
            return pb;
        } catch (SQLException e) {
            try {
                JdbcUtils.rollbackTransaction();
            } catch (SQLException e1) {}
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询所有订单
     * @param pc
     * @return
     */
    public PageBaen<Order> findAll(int pc){
        try {
            JdbcUtils.beginTransaction();
            PageBaen<Order> pb = orderDao.findAll(pc);
            JdbcUtils.commitTransaction();
            return pb;
        } catch (SQLException e) {
            try {
                JdbcUtils.rollbackTransaction();
            } catch (SQLException e1) {}
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 更新订单状态
     * @param oid
     * @param status
     */
    public void updateStatus(String oid, int status){
        try {
            orderDao.updateStatus(oid, status);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    /**
     * 查询订单状态
     * @param oid
     * @return
     */
    public int findStatus(String oid){
        try {
            return orderDao.findStatus(oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 加载订单
     * @param oid
     * @return
     */
    public Order load(String oid){
        try {
            return orderDao.load(oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 生成订单
     * @param order
     */
    public void createOrder(Order order){
        try {
            JdbcUtils.beginTransaction();
            orderDao.addOrder(order);
            JdbcUtils.commitTransaction();
        } catch (SQLException e) {
            try {
                JdbcUtils.rollbackTransaction();
            } catch (SQLException e1) {}
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    /**
     * 我的订单
     * @param uid
     * @param pc
     * @return
     */
    public PageBaen<Order> myOrders(String uid, int pc){
        try {
            JdbcUtils.beginTransaction();
            PageBaen<Order> pb = orderDao.findByUser(uid, pc);
            JdbcUtils.commitTransaction();
            return pb;
        } catch (SQLException e) {
            try {
                JdbcUtils.rollbackTransaction();
            } catch (SQLException e1) {}
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


}
